package org.moonlight.jvm.natives.java.lang.clazz;

import org.moonlight.jvm.natives.AbstractNativeMethod;
import org.moonlight.jvm.rtda.exclusive.Frame;
import org.moonlight.jvm.rtda.share.Object;
import org.moonlight.jvm.rtda.share.heap.ClassLoader;
import org.moonlight.jvm.rtda.share.methodarea.StringPool;

/**
 * java.lang.Class native getPrimitiveClass(String name)
 *
 * @author Moonlight
 * @createTime 2023/9/20 18:20
 **/
public class GetPrimitiveClass extends AbstractNativeMethod {

    public GetPrimitiveClass() {
        super(JAVA_LANG_CLASS, "getPrimitiveClass", "(Ljava/lang/String;)Ljava/lang/Class;");
    }

    @Override
    public void nativeMethod(Frame frame) {
        Object nameObj = frame.getLocalVars().getThis();
        String name = StringPool.javaString(nameObj);

        ClassLoader loader = frame.getMethod().getClazz().getLoader();
        Object jClass = loader.loadClass(name).getJClass();

        frame.getOperandStack().pushRef(jClass);
    }

}
